import { inject, type InjectionKey } from 'vue'
import { BreakpointStore } from './BreakpointStore'
import { PlainStore } from './PlainStore'
import { ClassificationStore } from './ClassificationStore'
import { RandomTestStore } from './RandomTestStore'
import { RandomTestingPageType } from '../constants/RandomTesting'
import { PageConfigStore } from './PageConfigStore'
import { InterviewQuestionsEditFormStore } from './InterviewQuestionsEditFormStore'
import { InterviewQuestionsFormStore } from './InterviewQuestionsFormStore'
import { RenderQuestionStore } from './RenderQuestionStore'

export class RandomTestingStore {
  constructor() {
    this.plainStore = new PlainStore(this)
    this.breakpointStore = new BreakpointStore(this)
    this.classificationStore = new ClassificationStore(this)
    this.randomTestStore = new RandomTestStore(this)
    this.renderQuestionStore = new RenderQuestionStore(this)
    this.pageConfigStore = new PageConfigStore(this)
    this.interviewQuestionsEditFormStore = new InterviewQuestionsEditFormStore(this)
    this.interviewQuestionsEditFormDataStore = new InterviewQuestionsFormStore(this)
  }

  init = async () => {
    const { plainStore } = this
    this.classificationStore
      .init()
      .then(() => {
        this.randomTestStore.init()
        this.randomTestStore.initQuestion().then(() => {
          this.renderQuestionStore.syncRenderData()
        })
      })
      .catch(() => {
        plainStore.pageType.value = RandomTestingPageType.ErrorPage
      })
  }

  /**
   * 普通数据
   */
  plainStore
  /**
   * 断点数据
   */
  breakpointStore
  /**
   * 分类数据
   */
  classificationStore
  /**
   * 随机测试题数据
   */
  randomTestStore
  /**
   * 渲染的题目数据
   */
  renderQuestionStore
  /**
   * 页面配置数据
   */
  pageConfigStore

  /**
   * 题目修改表单数据
   */
  interviewQuestionsEditFormStore
  /**
   * 题目修改表单内容数据
   */
  interviewQuestionsEditFormDataStore
}

export const RandomTestingStoreKey = Symbol() as InjectionKey<RandomTestingStore>

export const useRandomTestingStore = () => {
  return inject(RandomTestingStoreKey) as RandomTestingStore
}
